package service;

import dao.UsersManagement;
import dao.UsersManager;
import model.QQ;
import model.User;
import model.Wechat;
import security.Irreversible;
import security.MD5Hex;
import util.Common;
import java.util.List;

import static dao.QQsManager.qqMan;
import static dao.WechatsManager.wechatMan;


public class AccountServiceImp implements AccountService {

    private static final String defaultUserState = "启用";
    private static final Irreversible<String, String> encrypter = new MD5Hex();

    private final UsersManagement userMan;
    public AccountServiceImp(UsersManagement userManagement) {
        userMan = userManagement;
    }

    /**
     * 用户验证服务
     * @param username 用户名
     * @param password 口令
     * @return 登录状态
     */
    @Override
    public LoginState verificate(String username, String password) {

        User u = new User();
        u.setU_name(username);

        List<User> registed = userMan.query(u);

        if(registed == null  ||  registed.size() == 0) return LoginState.FAIL;
        u = registed.get(0);

        switch (u.getU_state()) {
            case "启用": return encrypter.encrypt(password).equals(u.getU_passwd())? LoginState.SUCCEED: LoginState.FAIL;
            case "停用": return LoginState.ACCOUNTDISABLED;
            case "封禁": return LoginState.ACCOUNTBANNED;
            default:     return LoginState.EXCEPTION;
        }
    }

    /**
     * 用户验证服务
     * @param uid      用户id
     * @param password 口令
     * @return 登录状态
     */
    @Override
    public LoginState verificate(Long uid, String password) {

        User u = new User();
        u.setU_id(uid);

        List<User> registed = userMan.query(u);

        if(registed == null  ||  registed.size() == 0) return LoginState.FAIL;
        u = registed.get(0);

        switch (u.getU_state()) {
            case "启用": return encrypter.encrypt(password).equals(u.getU_passwd())? LoginState.SUCCEED: LoginState.FAIL;
            case "停用": return LoginState.ACCOUNTDISABLED;
            case "封禁": return LoginState.ACCOUNTBANNED;
            default:     return LoginState.EXCEPTION;
        }
    }

    /**
     * 用户注册服务
     * @param userName 用户名
     * @param userPassword 口令
     * @param userNickname 昵称
     * @return 操作的结果
     */
    @Override
    public UsersManager.UserDAOOptionResult register(String userName, String userPassword, String userNickname) {

        User u = new User();
        u.setU_name(userName);
        u.setU_passwd(encrypter.encrypt(userPassword));
        u.setU_nick(userNickname);
        u.setU_state(defaultUserState);

        return userMan.register(u);
    }

    @Override
    public LoginInfo login(String uname, String upass){

        User emptyUserInfo = new User();
        emptyUserInfo.setU_name(uname);
        LoginInfo loginfo = new LoginInfo(null, emptyUserInfo, null, null);

        //若用户名或口令为空，返回失败信息
        if(Common.isStringEmpty(uname)  ||  Common.isStringEmpty(upass)) {
            loginfo.setLoginState(LoginState.FAIL);
            return loginfo;
        }

        //查询用户基本信息
        User u = new User();
        u.setU_name(uname);
        List<User> registed = userMan.query(u);

        //若未查到该用户，回应失败信息
        if(registed == null  ||  registed.size() == 0) {
            loginfo.setLoginState(LoginState.FAIL);
            return loginfo;
        }
        u = registed.get(0);

        //判断用户账户状态
        switch (u.getU_state()) {
            case "启用":
                if(!encrypter.encrypt(upass).equals(u.getU_passwd())) {
                    loginfo.setLoginState(LoginState.FAIL);
                    return loginfo;
                }
                u.setU_passwd(upass);
                break;

            case "停用": loginfo.setLoginState(LoginState.ACCOUNTDISABLED);
                return loginfo;

            case "封禁": loginfo.setLoginState(LoginState.ACCOUNTBANNED);
                return loginfo;

            default:     loginfo.setLoginState(LoginState.EXCEPTION);
                return loginfo;
        }

        loginfo.setLoginState(LoginState.SUCCEED);
        loginfo.setUserInfo(u);

        //查询用户QQ
        QQ thisUserQQ = new QQ();
        thisUserQQ.setU_id(u.getU_id());
        List<QQ> userQQ = qqMan.query(thisUserQQ);
        if(userQQ != null  &&  userQQ.size() == 1) loginfo.setUserQQ(userQQ.get(0).getQq_id());

        //查询用户微信
        Wechat thisUserWeChat = new Wechat();
        thisUserWeChat.setU_id(u.getU_id());
        List<Wechat> userWechat = wechatMan.query(thisUserWeChat);
        if(userWechat != null  &&  userWechat.size() == 1) loginfo.setUserWechat(userWechat.get(0).getWechat_id());

        return loginfo;
    }
}

/*
 * 用于Servlet的账号验证服务
 * @param request Servlet请求
 * @return 成功返回true，失败返回false
 * @throws ServletException
 * @throws IOException
 *//*
    @Override
    public boolean login(HttpServletRequest request, ServerResponse responData) throws ServletException, IOException {

        String uname = request.getParameter("username");
        String upass = request.getParameter("password");

        //System.out.println("upass:" + upass);

        boolean isSucceed = false;
        switch (verificate(uname, upass)){
            case FAIL:            responData.add("OESloginStat", "fail");
                break;
            case ACCOUNTDISABLED: responData.add("OESloginStat", "accountdisabled");
                break;
            case ACCOUNTBANNED:   responData.add("OESloginStat", "accountbanned");
                break;
            case EXCEPTION:       responData.add("OESloginStat", "loginexception");
                break;
            case SUCCEED:         responData.add("OESloginStat", "succeed");
                isSucceed = true;
        }

        return isSucceed;
    }*/